package com.home.activities;

import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.home.rhclient.MainController;
import com.home.rhtester.R;

public class ListCategoriesActivity extends AdvanceActivity {

	private ListView categoryList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.categorieslayout);
		categoryList = (ListView) findViewById(R.id.categorylist);
		try {
			MainController.GetInstance().restConnector
					.requestListofResourceCategories(new JSONObject(),
							categoryList);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		categoryList.setOnItemClickListener(new CategoryListener());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.listview_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.resfresh_list:
			try {
				MainController.GetInstance().restConnector
						.requestListofResourceCategories(new JSONObject(),
								categoryList);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			break;
		}
		return true;
	}

	class CategoryListener implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			Intent i = new Intent(ListCategoriesActivity.this,
					ListResourcesActivity.class);
			i.putExtra("id", id);
			ListCategoriesActivity.this.startActivity(i);
		}
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

}
